﻿CREATE FUNCTION [QIC].[TokenDeclare](@Token QIC.TokenTable READONLY, @ObjectId INT, @Indent INT)
RETURNS VARCHAR(MAX)
AS
BEGIN

	DECLARE @ReturnValue VARCHAR(MAX)
	DECLARE @XMLColumns VARCHAR(MAX)
	DECLARE @MissingProperty QIC.TokenTable 
	DECLARE @IsView bit

	IF EXISTS (select * from sys.objects where object_id = @ObjectId and type = 'V' ) SET @IsView = 1
	ELSE SET @IsView = 0

	-- MISSING PROPERTY
	BEGIN
		INSERT INTO @MissingProperty (ObjectId,PropertyId,PropertyName,UserTypeId,Precision,Scale,MaxLength,IsOutPut,IsReadOnly)
		SELECT 
			0,
			0,
			property.PropertyName,
			property.UserTypeId,
			property.Precision,
			property.Scale,
			property.MaxLength,
			0,
			0
		FROM @Token property
		INNER JOIN 
			(
				SELECT 
					missingproperty.Id,
					ROW_NUMBER() OVER (PARTITION BY missingproperty.PropertyName Order by missingproperty.Id) Ranking
				FROM @Token missingproperty
				LEFT OUTER JOIN @Token interfaceproperty
					ON interfaceproperty.ObjectId = @ObjectId
					AND interfaceproperty.PropertyName = missingproperty.PropertyName
					AND interfaceproperty.PropertyId > 0
				WHERE interfaceproperty.Id is null
			) missingproperty
			ON missingproperty.Id = property.Id
			AND Ranking = 1
			AND property.PropertyName like '@%'		
		ORDER BY property.Id
	
		SET @ReturnValue = ''

		SELECT @ReturnValue += 
			QIC.Indent(@Indent) + 'DECLARE ' +
			QIC.TokenDefine(PropertyName, UserTypeId, Precision, Scale, MaxLength, IsOutPut, IsReadOnly) +
			QIC.LB()
		FROM @MissingProperty missingproperty
		ORDER BY missingproperty.Id
	END

	-- SPECIAL PROPERTY
	BEGIN

		IF EXISTS (select * from @MissingProperty where PropertyName = '@InterfaceId') 
			SET @ReturnValue += QIC.Indent(@Indent) + 'SET @InterfaceId = ' + CAST(@ObjectId as varchar(max)) + QIC.LB()
		IF EXISTS (select * from @MissingProperty where PropertyName = '@InterfaceName') 
			SET @ReturnValue += QIC.Indent(@Indent) + 'SET @InterfaceName = ' + QUOTENAME(OBJECT_NAME(@ObjectId),'''') + QIC.LB()

		IF EXISTS (select * from @MissingProperty where PropertyName = '@InterfaceContext') 
		BEGIN
			IF @IsView = 1
			BEGIN
				SET @XMLColumns = ''

				SELECT @XMLColumns += QUOTENAME(PropertyName) + ' AS ' + QUOTENAME(REPLACE(PropertyName,' ','-')) + ',' from @Token where ObjectId = @ObjectId order by PropertyId

				SET @XMLColumns = LEFT(@XMLColumns, DATALENGTH(@XMLColumns) -1)

				SET @ReturnValue += QIC.Indent(@Indent) + 'SET @InterfaceContext = (SELECT '
				SELECT @ReturnValue += '(SELECT ' + @XMLColumns+ ' FROM Inserted FOR XML PATH(''Inserted''), TYPE) Inserted, (SELECT ' + @XMLColumns+ ' FROM Deleted FOR XML PATH(''Deleted''), TYPE) Deleted FOR XML PATH(''InterfaceContext'')'
				SET @ReturnValue += ')' + QIC.LB()
			END
			ELSE
			BEGIN
				SET @ReturnValue += QIC.Indent(@Indent) + 'SET @InterfaceContext = (SELECT '
			
				SELECT @ReturnValue += 
					CASE types.is_table_type 
						WHEN 1 THEN '(select * from ' + token.PropertyName + ' FOR XML PATH(''' + TYPE_NAME(types.user_type_id) + '''), TYPE)'
						ELSE token.PropertyName
					END +
					' ' + QUOTENAME(REPLACE(token.PropertyName,'@','')) + ', '
				FROM @Token token
				LEFT OUTER JOIN sys.types types
					on types.user_type_id = token.UserTypeId
				WHERE token.ObjectId = @ObjectId
				AND token.PropertyId > 0
				ORDER BY token.Id

				IF @@ROWCOUNT <> 0 
				BEGIN
					SET @ReturnValue = LEFT(@ReturnValue,DATALENGTH(@ReturnValue) - 2)
				END
				ELSE
				BEGIN
					SET @ReturnValue += 'NULL'
				END

				SET @ReturnValue += ' FOR XML PATH(''InterfaceContext''))' + QIC.LB()

			END
		END

	END

	IF @ReturnValue <> '' SET @ReturnValue = LEFT(@ReturnValue,DATALENGTH(@ReturnValue) - 2)

	RETURN @ReturnValue

END